package com.tsm.picachu.common;

import java.util.Random;

import com.tsm.picachu.data.User;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.DisplayMetrics;

public final class Utils
{
	private Utils()
	{
	}

	/** Get the random number and less than roundNumber */
	public static int getNumberRandom(int roundNumber)
	{
		Random random = new Random();
		int number = random.nextInt(roundNumber);
		return number;
	}

	public static String getTimeStringFromSecond(int second)
	{
		int minute = second / 60;
		int secondRemain = second - 60 * minute;
		int hour = minute / 60;

		String minuteStr = String.format("%02d", minute);
		String secondStr = String.format("%02d", secondRemain);
		String hourStr = String.format("%02d", hour);

		return hourStr + ":" + minuteStr + ":" + secondStr;
	}

	/**
	 * Get the height of the notification bar
	 * 
	 * @param activity
	 * @return the height in pixels
	 */
	public static int getNotifictionBarHeight(Activity activity)
	{
		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

		switch (metrics.densityDpi)
		{
		case DisplayMetrics.DENSITY_HIGH:
			return 48;
		case DisplayMetrics.DENSITY_MEDIUM:
			return 32;
		case DisplayMetrics.DENSITY_LOW:
			return 24;
		default:
			return 64;
		}
	}

	public static void savePreference(Context context, String preferenceName,
			String preferenceKey, String value)
	{
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				preferenceName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(preferenceKey, value);
		editor.commit();
	}

	public static String getPreference(Context context, String preferenceName,
			String preferenceKey)
	{
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				preferenceName, Context.MODE_PRIVATE);
		return sharedPreferences.getString(preferenceKey, "");
	}

	public static User getUserData(Context context, String username)
	{
		User user = new User();
		user.setName(username);

		SharedPreferences sharedPreferences = context.getSharedPreferences(
				username, Context.MODE_PRIVATE);

		String hightScoreString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_HIGHSCORE,
				Constant.PREFERENCE_DEFAULT_VALUE);
		if (!TextUtils.isEmpty(hightScoreString))
		{
			user.setHighScore(Integer.parseInt(hightScoreString));
		}

		String musicString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_IS_MUSIC,
				Constant.PREFERENCE_IS_MUSIC_ON);
		user.setMusic(musicString.equals(Constant.PREFERENCE_IS_MUSIC_ON));

		String levelString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_LEVEL,
				Constant.PREFERENCE_DEFAULT_VALUE);
		if (!TextUtils.isEmpty(levelString))
		{
			user.setLevel(Integer.parseInt(levelString));
		}

		String humanString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_HUMAN,
				Constant.PREFERENCE_DEFAULT_VALUE);
		if (!TextUtils.isEmpty(humanString))
		{
			user.setHuman(Integer.parseInt(humanString));
		}

		String miniLevelString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_MINI_LEVEL,
				Constant.PREFERENCE_DEFAULT_VALUE);
		if (!TextUtils.isEmpty(miniLevelString))
		{
			user.setMiniLevel(Integer.parseInt(miniLevelString));
		}

		String scoreString = sharedPreferences.getString(
				Constant.PREFERENCE_KEY_SCORE,
				Constant.PREFERENCE_DEFAULT_VALUE);
		if (!TextUtils.isEmpty(scoreString))
		{
			user.setScore(Integer.parseInt(scoreString));
		}

		return user;
	}

	public static void saveUser(Context context, User user)
	{
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				user.getName(), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();

		editor.putString(Constant.PREFERENCE_KEY_HIGHSCORE, user.getHighScore()
				+ "");
		editor.putString(
				Constant.PREFERENCE_KEY_IS_MUSIC,
				user.isMusic() ? Constant.PREFERENCE_IS_MUSIC_ON : Constant.PREFERENCE_IS_MUSIC_OFF);
		editor.putString(Constant.PREFERENCE_KEY_LEVEL, user.getLevel() + "");
		editor.putString(Constant.PREFERENCE_KEY_HUMAN, user.getHuman() + "");
		editor.putString(Constant.PREFERENCE_KEY_MINI_LEVEL,
				user.getMiniLevel() + "");
		editor.putString(Constant.PREFERENCE_KEY_SCORE, user.getScore() + "");

		editor.commit();
	}
}
